package com.ksd.modules.note.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.modules.note.entity.AdminNote;
import com.ksd.modules.note.po.AdminNoteSavePO;
import com.ksd.modules.note.po.AdminNoteSearchPO;
import com.ksd.modules.note.service.IAdminNoteService;
import com.ksd.modules.note.vo.AdminNoteVo;
import com.ksd.redis.util.RedisUtil_Back;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class AdminNoteController extends BaseController {
    
    @Autowired
    private IAdminNoteService adminNoteService;
    @Autowired
    private RedisUtil_Back redisUtilBack;
    private static final String CACHE_CHANNEL_KEY1 = "KSD:SYSTEM:NOTE:GetNoteDetailByID:";
    private static final String CACHE_CHANNEL_KEY2 = "KSD:SYSTEM:NOTE:FindPushNoteListAndPage:";
    /**
     * 【小册】- 列表
     * @return
     */
    @PostMapping("/note/list")
    public R<Page<AdminNoteVo>> findAdminNotes(@Validated @RequestBody AdminNoteSearchPO adminNoteSearchPO){
        return R.data(adminNoteService.findAdminNotes(adminNoteSearchPO));
    }

    /**
     * 【小册】- 回收站
     * @return
     */
    @PostMapping("/note/recyle")
    public R<Page<AdminNoteVo>> findAdminNotesRecyle(@Validated @RequestBody AdminNoteSearchPO adminNoteSearchPO){
        return R.data(adminNoteService.findAdminNotesRecyle(adminNoteSearchPO));
    }

    /**
     * 【小册】 - 保存和更新
     * @param adminNoteSavePO
     * @return
     */
    @PostMapping("/note/saveupdate")
    @ApiLog("【小册】 - 保存和更新")
    @RepeatSubmit
    public R<AdminNoteVo> saveUpdateAdminNote(@Validated @RequestBody AdminNoteSavePO adminNoteSavePO){
        return R.data(adminNoteService.saveUpdateAdminNote(adminNoteSavePO));
    }

    /**
     * 【小册】 - 根据id查询明细
     * @param id
     * @return
     */
    @PostMapping("/note/get/{id}")
    public R<AdminNoteVo> getAdminNoteById(@PathVariable("id")Long id){
        return R.data(adminNoteService.getAdminNoteById(id));
    }

    /**
     * 【小册】 - 根据id删除小册
     * @param id
     * @return
     */
    @PostMapping("/note/del/{id}")
    @ApiLog("【小册】 - 根据id删除小册 ")
    public R<Boolean> deleteAdminNoteById(@PathVariable("id")Long id){
        return R.data(adminNoteService.deleteAdminNoteById(id));
    }

    /**
     * 【小册】- 根据id恢复删除
     *
     * @param id
     * @return
     */
    @PostMapping("/note/recover/{id}")
    @ApiLog("【小册】- 根据id恢复删除")
    @RepeatSubmit
    public R<Boolean> recoverByIdAdminNote(@PathVariable("id") Long id) {
        return R.data(adminNoteService.recoverByIdAdminNote(id));
    }

    /**
     * 【小册】- 批量逻辑删除
     *
     * @return
     */
    @PostMapping("/note/batch/dels")
    @ApiLog("【小册】- 批量逻辑删除")
    @RepeatSubmit
    public R<Boolean> bacthDeletesAdminNote(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminNoteService.bacthDeletesAdminNote(batchIdsPO));
    }


    /**
     * 【小册】- 批量恢复
     *
     * @return
     */
    @PostMapping("/note/batch/recover")
    @ApiLog("【小册】- 批量恢复")
    @RepeatSubmit
    public R<Boolean> batchRecoversAdminNote(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminNoteService.batchRecoversAdminNote(batchIdsPO));
    }
    
    /**
     * 【小册】- 批量物理删除
     *
     * @return
     */
    @PostMapping("/note/batch/remove")
    @ApiLog("【小册】- 批量物理删除")
    @RepeatSubmit
    public R<Boolean> batchRemovesAdminNote(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminNoteService.batchRemovesAdminNote(batchIdsPO));
    }

    /**
     * 【小册】- 根据ID恢复删除
     * @param id
     * @return
     */
    @PostMapping("/note/remove/{id}")
    @ApiLog("【小册】- 根据ID恢复删除")
    public R<Boolean> removeAdminNoteById(@PathVariable("id")Long id){
        return R.data(adminNoteService.removeAdminNoteById(id));
    }

    /**
     * 【小册】- 修改状态
     *
     * @return
     */
    @PostMapping("/note/status/{id}/{field}")
    @ApiLog("【小册】- 修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminNoteService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminNoteService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【小册】-批量上架
     *
     * @return
     */
    @PostMapping("/note/batch/up")
    @ApiLog("批量上架")
    @RepeatSubmit
    @ApiOperation("【小册】-批量上架")
    public R<Boolean> batchUps(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminNoteService.batchUps(batchIdsPO));
    }

    /**
     * 【小册】-批量下架
     *
     * @return
     */
    @PostMapping("/note/batch/down")
    @ApiLog("【小册】-批量下架")
    @RepeatSubmit
    public R<Boolean> batchDowns(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminNoteService.batchDowns(batchIdsPO));
    }

    /**
     * 【专题】-刷新缓存
     *
     * @return
     */
    @PostMapping("/note/refresh/{id}")
    @ApiLog("【小册】-刷新缓存")
    @RepeatSubmit
    public R<Boolean> noteRefresh(@PathVariable("id")Long id) {
        AdminNote adminNote = adminNoteService.getById(id);
        if(adminNote!=null) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + id + ":" + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return R.data(true);
    }


    /**
     * 【小册】-【章】-  复制数据
     * @param opid
     * @return
     */
    @PostMapping("/note/copy/{opid}")
    @ApiLog("【小册】-复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminNoteService.copyData(opid));
    }
}
